﻿using Microsoft.Extensions.DependencyInjection;
using RadioTimer.Services;

namespace RadioTimer
{
    /// <summary>
    /// 应用程序的主类
    /// 继承自Application，作为MAUI应用程序的根类
    /// 负责应用程序的初始化和窗口创建
    /// 定义应用程序的全局资源和启动行为
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// ServiceProvider实例，用于获取服务
        /// </summary>
        public static IServiceProvider ServiceProvider { get; private set; }

        /// <summary>
        /// 初始化App实例
        /// 调用InitializeComponent方法加载XAML中定义的资源
        /// </summary>
        public App()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 创建应用程序窗口
        /// 该方法在应用程序启动时被调用，返回主窗口实例
        /// 使用Shell作为主页面，通过TabBar实现导航
        /// </summary>
        /// <param name="activationState">应用程序激活状态信息</param>
        /// <returns>返回包含MainPage(TabbedPage)的主窗口实例</returns>
        protected override Window CreateWindow(IActivationState? activationState)
        {
            // 获取ServiceProvider
            ServiceProvider = IPlatformApplication.Current!.Services;
            
            // 初始化DebugLogger
            var logService = ServiceProvider.GetService<ILogService>();
            if (logService != null)
            {
                DebugLogger.Initialize(logService);
            }
            
            // 直接创建并返回包含MainPage的新窗口
            // MainPage会通过依赖注入自动解析所需的子页面
            var window = new Window(new AppShell());
            
            // 订阅窗口生命周期事件
            window.Created += OnWindowCreated;
            window.Activated += OnWindowActivated;
            window.Deactivated += OnWindowDeactivated;
            window.Stopped += OnWindowStopped;
            window.Resumed += OnWindowResumed;
            window.Destroying += OnWindowDestroying;
            
            return window;
        }

        #region 应用程序生命周期方法

        /// <summary>
        /// 应用程序启动时调用
        /// 在CreateWindow之前调用，用于全局初始化
        /// </summary>
        protected override void OnStart()
        {
            base.OnStart();
            Services.DebugLogger.WriteLine("[App Lifecycle] OnStart - 应用程序启动");
        }

        /// <summary>
        /// 应用程序进入睡眠状态时调用
        /// 在应用切换到后台时触发，可用于保存状态和释放资源
        /// </summary>
        protected override void OnSleep()
        {
            base.OnSleep();
            Services.DebugLogger.WriteLine("[App Lifecycle] OnSleep - 应用程序进入后台");
        }

        /// <summary>
        /// 应用程序从睡眠状态恢复时调用
        /// 在应用从后台返回前台时触发，可用于恢复状态和重新初始化
        /// </summary>
        protected override void OnResume()
        {
            base.OnResume();
            Services.DebugLogger.WriteLine("[App Lifecycle] OnResume - 应用程序恢复到前台");
        }

        #endregion

        #region 窗口生命周期事件处理

        /// <summary>
        /// 窗口创建时触发
        /// 在窗口首次创建后调用
        /// </summary>
        private void OnWindowCreated(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Created - 窗口已创建");
        }

        /// <summary>
        /// 窗口激活时触发
        /// 在窗口获得焦点时调用
        /// </summary>
        private void OnWindowActivated(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Activated - 窗口已激活");
        }

        /// <summary>
        /// 窗口失活时触发
        /// 在窗口失去焦点时调用
        /// </summary>
        private void OnWindowDeactivated(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Deactivated - 窗口已失活");
        }

        /// <summary>
        /// 窗口停止时触发
        /// 在应用进入后台时调用
        /// </summary>
        private void OnWindowStopped(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Stopped - 窗口已停止");
        }

        /// <summary>
        /// 窗口恢复时触发
        /// 在应用从后台恢复时调用
        /// </summary>
        private void OnWindowResumed(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Resumed - 窗口已恢复");
        }

        /// <summary>
        /// 窗口销毁时触发
        /// 在窗口即将销毁时调用，用于清理资源
        /// </summary>
        private void OnWindowDestroying(object? sender, EventArgs e)
        {
            Services.DebugLogger.WriteLine("[Window Lifecycle] Destroying - 窗口正在销毁");
            
            // 取消订阅事件，防止内存泄漏
            if (sender is Window window)
            {
                window.Created -= OnWindowCreated;
                window.Activated -= OnWindowActivated;
                window.Deactivated -= OnWindowDeactivated;
                window.Stopped -= OnWindowStopped;
                window.Resumed -= OnWindowResumed;
                window.Destroying -= OnWindowDestroying;
            }
        }

        #endregion
    }
}